/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.materialdrawer;

import me.panpf.sketch.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Navigation Drawer section with Material Design style
 *
 * @since 2021.6.29
 */
public class DrawerItem implements Component.ClickedListener {
    /**
     * MENU_HEADER
     */
    public static final int MENU_HEADER = 0;

    /**
     * MENU_ITEM
     */
    public static final int MENU_ITEM = 1;

    /**
     * MENU_DISABLE_ITEM
     */
    public static final int MENU_DISABLE_ITEM = 2;
    private Component cv_root;
    private Text tv_header;
    private Text tv_item;
    private Component div_header;
    private DrawerItemListener listener;
    private boolean isTouchAble = false;
    private String title;
    private Fraction fraction;
    private Context context;

    public DrawerItem(Context context, String title, Fraction fraction, int menuType) {
        this.context = context;
        this.title = title;
        this.fraction = fraction;
        cv_root = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_menu_item, null, false);
        tv_header = (Text) cv_root.findComponentById(ResourceTable.Id_section_header);
        tv_item = (Text) cv_root.findComponentById(ResourceTable.Id_section_item);
        div_header = cv_root.findComponentById(ResourceTable.Id_section_div);
        switch (menuType) {
            case MENU_HEADER:
                div_header.setVisibility(Component.VISIBLE);
                tv_header.setVisibility(Component.VISIBLE);
                tv_item.setVisibility(Component.HIDE);
                tv_header.setText(title);
                break;
            case MENU_ITEM:
                div_header.setVisibility(Component.INVISIBLE);
                tv_header.setVisibility(Component.HIDE);
                tv_item.setVisibility(Component.VISIBLE);
                tv_item.setText(title);
                isTouchAble = true;
                break;
            case MENU_DISABLE_ITEM:
                div_header.setVisibility(Component.INVISIBLE);
                tv_header.setVisibility(Component.HIDE);
                tv_item.setVisibility(Component.VISIBLE);
                tv_item.setText(title);
                tv_item.setTextColor(new Color(context.getColor(ResourceTable.Color_disable)));
                break;
        }
        cv_root.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component view) {
        if (isTouchAble) {
            cv_root.setBackground(new ShapeElement(context, ResourceTable.Graphic_shape_item_pressed));
            context.getUITaskDispatcher().delayDispatch(() -> {
                cv_root.setBackground(new ShapeElement(context, ResourceTable.Graphic_shape_item_normal));
                afterClick();
            }, 200);
        }
    }

    /**
     * setOnClickListener
     *
     * @param listener DrawerItemListener
     */
    public void setOnClickListener(DrawerItemListener listener) {
        this.listener = listener;
    }

    /**
     * getView
     *
     * @return Component Component
     */
    public Component getView() {
        return cv_root;
    }

    public String getTitle() {
        return title;
    }

    public Fraction getFraction() {
        return fraction;
    }

    private void afterClick() {
        if (listener != null) {
            listener.onClick(this);
        }
    }

}
